package com.green.voluntaryfiling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.green.voluntaryfiling.domain.User;
import com.green.voluntaryfiling.mapper.UserMapper;
import com.green.voluntaryfiling.service.UserService;
import com.green.voluntaryfiling.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * @Author: Green
 * @Date: 2022/03/14/23:45
 * @Description: 
 */


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    private UserMapper userMapper;

    /**
     * 存储用户信息
     * @param userVo
     * @return
     */
    @CachePut(cacheNames = "com.green.voluntaryfiling.service.impl.UserServiceImpl",key = "#result.id")
    @Override
    public User saveUser(UserVo userVo) {
        User user = new User();
        user.setUsername(userVo.getUsername());
        user.setArea(userVo.getArea());
        user.setUserimg(userVo.getUserImg());
        user.setStRanking(userVo.getSt_ranking());
        user.setSubject(userVo.getSubject());
        user.setStScore(userVo.getSt_score());
        //此处新增
        user.setMainSubject(userVo.getMainSubject());
        user.setSecondSubject(userVo.getSecond_subject());
        user.setThirdSubject(userVo.getThird_subject());

        user.setSearchTimes(1);
        this.userMapper.insert(user);
        return user;
    }


    /**
     * 判断当前用户是否存在数据库，存在返回存储键值，不存在返回-1
     * @param username
     * @return
     */
    @Override
    public User isExist(String username) {
        IPage<User> page = new Page<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(username), "username",username);

        this.userMapper.selectPage(page,queryWrapper);
        List<User> userList = page.getRecords();
        for (User user : userList) {
            if (user.equals("") || user.equals(null) || user.equals(0)){
                return null;
            }
            return user;
        }
        return null;

    }

    /**
     * 更新当前用户数据信息
     * @param user,userVo
     * @return
     */
    @CachePut(cacheNames = "com.green.voluntaryfiling.service.impl.UserServiceImpl",key = "#result.id")
    @Override
    public User updateByUser(User user,UserVo userVo) {

        user.setUserimg(userVo.getUserImg());
        user.setUsername(userVo.getUsername());
        user.setStRanking(userVo.getSt_ranking());
        user.setStScore(userVo.getSt_score());
        user.setSubject(userVo.getSubject());
        user.setArea(userVo.getArea());
        //此处新增
        user.setMainSubject(userVo.getMainSubject());
        user.setSecondSubject(userVo.getSecond_subject());
        user.setThirdSubject(userVo.getThird_subject());
        //访问次数+1
        user.setSearchTimes(user.getSearchTimes()+1);
        userMapper.updateById(user);
        return user;
    }


    /**
     * 更新当前用户数据信息，再次登录不输入信息情况
     * @param user
     * @return
     */
    @CachePut(cacheNames = "com.green.voluntaryfiling.service.impl.UserServiceImpl",key = "#result.id")
    @Override
    public User updateByUser(User user) {
        //访问次数+1
        user.setSearchTimes(user.getSearchTimes()+1);
        userMapper.updateById(user);
        return user;
    }


}
